package com.ruins.ping.consumer;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruins.ping.vo.Log;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class LogReceiver {

    private final MongoTemplate mongoTemplate;

    @KafkaListener(topics = "logTopic", groupId = "logGroup")
    public void receiveLog(String message) {
        JSONObject data = JSONObject.parseObject(message);

        // 异步记录日志信息
        Log log = new Log();
        log.setPid(data.get("pid").toString());
        log.setMessage(data.get("logMessage").toString());
        log.setCreateTime(DateUtil.now());
        mongoTemplate.save(log);
    }
}
